<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\communityService;
use app\service\roominfoService;
use think\Db;
class Device extends Common{
	public function deviceList(){
		$category_id=request()->get("category_id");
		//var_dump(request()->get());
		if(empty($category_id)){
			return "页面错误";
		}
		$com = $this->pidTokenId();
		$whe['community_id'] = array('in',$com['community_id']);
		$communitylist=(new roominfoService())->communityList($whe);
		$this->assign('communitylist',$communitylist);

		$whe["category_id"]=$category_id;
		$category=Db::table("v2_category")->where("category_id",$category_id)->find();
		$devicelist=Db::table("v2_device")->where($whe)->order("id desc")->select();
		$this->assign("category",$category);
		$this->assign("list",$devicelist);
		return $this->fetch("v2_device/device_list");
	}

	public function deviceAdd(){
		if(request()->isPost()){
			$data=request()->post();
			$community=Db::name('community')->where('community_id',$data["community_id"])->find();
			$data["buy_time"]=strtotime($data["buy_time"]);
			$data["community_name"]=$community["community_name"];

			$res=Db::table("v2_device")->insert($data);
           	if($res){
           		$url=url('deviceAdd')."category_id=".$data["category_id"];
                $this->success('添加设备成功',$url);
            }else{
                $this->error('添加设备成功',$url);
            }
		}else{
			$category_id=request()->get("category_id");
			$com = $this->pidTokenId();
			$whe['community_id'] = array('in',$com['community_id']);
			$communitylist=(new roominfoService())->communityList($whe);
			$this->assign('communitylist',$communitylist);
			
			$category=Db::table("v2_category")->where("category_id",$category_id)->find();
			$this->assign("category",$category);
			return $this->fetch("v2_device/device_add");
		}
		
	}
}